<?php
namespace app\home\controller;

use \app\home\model\Member as MmbModel;
use \think\Session as Session;
class Member  extends Home {

    /**
     * 用户中心
     * @mid 会员id；
     */
    public function index(){
        
    }

    public function signup(){

        return $this->fetch();
    }

    public function register(){
        $email = $this->req->post('email');
        $pwd1 = $this->req->post('pwd1');
        $pwd2 = $this->req->post('pwd2');
        // $data['mmb_email'] = 
        var_dump($email);
        if(empty($email) || empty($pwd1) || empty($pwd2) || $pwd1 != $pwd2){
            exit();
        }{
            $pattern = "/^([0-9A-Za-z\\-_\\.]*)@([0-9a-z]*\\.[a-z]{2,6})$/i";
            if(preg_match($pattern,$email)){
                $model = new MmbModel;
                // $bool = $model->where('mmb_email','like','%'.$email.'%')->select();
                // if($bool){
                //     $this->error('用户名已存在');
                // }else{
                    if($length = strlen($pwd1) < 8){
                        $this->error('密码长度不能少于8位');
                    }else{
                        $data['mmb_email'] = $email;
                        $data['mmb_pwd'] = password_hash($pwd1,PASSWORD_DEFAULT);
                        if($mid = $model->save($data)){
                            // $this->redirect('member/index',['email'=>$email,'token'=> md5(uniqid(rand(), true))]);
                            $token = md5(uniqid(rand(), true));
                            Session::set('member.email',$email);
                            Session::set('member.token',$token);
                            $this->assign('email',$email);
                            return $this->fetch();
                        }else{
                            $this->error('未知错误，请查看日记');
                        }
                    }
                // }
            }
            
        }
    }

    public function regsuccess(){
        
    }



    private function generateHashWithSalt($password) {
        $intermediateSalt = md5(uniqid(rand(), true));
        $salt = substr($intermediateSalt, 0, 6); 
        return hash("sha256", $password . $salt);
    }

    public function forget(){
        
    }

}
